var path = require('path')
var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var autoprefixer = require('autoprefixer')

function getConfig(type, version) {
  var SERVERMODE = type === 'SERVER';

  const suffix = version ? `v${version}` : '';

  return config = {
    context: path.join(__dirname, 'app'),
    entry: {},
    output: {},
    devtool: 'source-map',
    externals: {
      'react': 'window.React',
      'react-dom': 'window.ReactDOM',
      'wx': 'window.wx',
      'raven-js': 'window.Raven'
    },
    module: {
      loaders: [
        {
          test: /\.js?$/,
          exclude: /node_modules/,
          loader: 'babel',
          query: {
            presets: ['es2015', 'es2016', 'stage-2', 'react']
          }
        },
        // CSS
        {
          test: /\.css$/,
          exclude: /node_modules/,
          loader: SERVERMODE
            ? 'style!css'
            : ExtractTextPlugin.extract('style-loader', 'css-loader')
        },
        // LESS
        {
          test: /\.less$/,
          exclude: /node_modules/,
          loader: SERVERMODE
            ? 'style!css?sourceMap!less?sourceMap'
            : ExtractTextPlugin.extract('style-loader', 'css!less')
        }]
    },
    plugins: SERVERMODE
      ? [new webpack.optimize.OccurenceOrderPlugin(), new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin()]
      : [new webpack.optimize.OccurenceOrderPlugin(), new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), new ExtractTextPlugin(`[name]${suffix}.min.css`)]
  }
}
module.exports = getConfig;